Skip to content

add compile_commands.json#24

Merged
Sunrisepeak merged 9 commits into
mcpp-community:mainfrom
yizhinailong:main
May 11, 2026
Merged

add compile_commands.json#24
Sunrisepeak merged 9 commits into
mcpp-community:mainfrom
yizhinailong:main

Conversation

@yizhinailong
Copy link
Copy Markdown
Contributor

No description provided.

yizhinailong and others added 6 commits May 10, 2026 23:37
Extract compute_cxxflags() and compute_cflags() from emit_ninja_string()
into standalone helper functions in the anonymous namespace. These helpers
encapsulate all flag computation logic (PIC, sysroot, binutils, musl opt,
include dirs, user flags) and will be reused by compile_commands.json
generation in a follow-up commit.

The ninja output is byte-identical to before the refactoring.
Automatically emit a Clang-compatible compile_commands.json to the
target/ directory on every mcpp build. The JSON includes all compile
units (project + dependencies) with exact compiler commands, matching
the flags used by the ninja backend.

- Add json_escape() helper for JSON string escaping
- Add emit_compile_commands_json() using shared flag helpers
- Wire into NinjaBackend::build() after build.ninja generation
Architecture redesign on top of PR mcpp-community#24's compile_commands.json feature:

1. `src/build/flags.cppm` (NEW) — shared compile/link flag computation.
   One definition consumed by both ninja backend and compile_commands
   emitter (and future backends). Eliminates the duplicated
   compute_cxxflags/compute_cflags functions.

2. `src/build/compile_commands.cppm` (NEW) — generates
   compile_commands.json using nlohmann::json (no manual string escaping).
   Uses `arguments` array format (clangd recommended). Writes to
   <projectRoot>/compile_commands.json for zero-config clangd.
   Incremental: skips write if content unchanged.

3. `src/libs/json.cppm` + `src/libs/json/json.hpp` (NEW) — nlohmann/json
   single-header library wrapped as C++23 module, copied from
   openxlings/xlings. MIT license.

4. `src/build/ninja_backend.cppm` — slimmed: imports flags.cppm,
   delegates compile_commands to its module, removes ~200 lines of
   duplicated flag computation.

5. `.agents/docs/2026-05-12-compile-commands-design.md` — design doc.

6. `.gitignore` — add compile_commands.json (generated).

7. `mcpp.toml` — add `include_dirs = ["src/libs/json"]` for json.hpp.

All code formatted with .clang-format from PR mcpp-community#24.
read_xpkg_lua now tries multiple filename patterns when looking up
a package's .lua descriptor in index repos:
  1. <full-qualified-name>.lua   (old: mcpplibs.cmdline.lua)
  2. <short-name>.lua            (new: cmdline.lua)
  3. compat.<name>.lua           (new: compat.gtest.lua)

This completes namespace migration support: both install_path (0.0.7)
and read_xpkg_lua (0.0.8) handle old and new index layouts.

Locally verified: mcpp self-build + tinyhttps + llmapi + cmdline all
pass against the migrated mcpp-index (namespace fields + short names
+ renamed files).
Copy link
Copy Markdown
Member

@Sunrisepeak Sunrisepeak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI passed, LGTM

@Sunrisepeak Sunrisepeak merged commit ebc26c6 into mcpp-community:main May 11, 2026
1 check passed
Sunrisepeak added a commit that referenced this pull request May 11, 2026
Squashed commits:

- 13928b0 refactor(build): extract flag construction into shared helpers
  Extract compute_cxxflags() and compute_cflags() from emit_ninja_string()
  into standalone helper functions. These helpers encapsulate all flag
  computation logic and will be reused by compile_commands.json generation.

- f9d4f95 feat(build): generate compile_commands.json during build
  Automatically emit a Clang-compatible compile_commands.json on every
  mcpp build. The JSON includes all compile units with exact compiler
  commands, matching the flags used by the ninja backend.

- 5c1da99 feat: add .clang-format from mcpp-community/mcpp-style-ref

- 9675ea9 refactor(build): extract flags + compile_commands into dedicated modules
  Architecture redesign: flags.cppm (shared flag computation),
  compile_commands.cppm (JSON generation via nlohmann/json),
  json.cppm + json.hpp (nlohmann/json single-header as C++23 module).
  ninja_backend.cppm slimmed by ~200 lines.

- 0134a8b feat: namespace-aware read_xpkg_lua + bump to 0.0.8
  read_xpkg_lua now tries multiple filename patterns for package .lua
  descriptors in index repos.

- 2f482b7 ci: bump xlings 0.4.25 → 0.4.30 (sysroot fix for gcc 16.1.0)

- eba834d ci: always install pinned xlings (cache may hold stale version)
Sunrisepeak added a commit that referenced this pull request May 11, 2026
Squashed commits:

- 13928b0 refactor(build): extract flag construction into shared helpers
  Extract compute_cxxflags() and compute_cflags() from emit_ninja_string()
  into standalone helper functions. These helpers encapsulate all flag
  computation logic and will be reused by compile_commands.json generation.

- f9d4f95 feat(build): generate compile_commands.json during build
  Automatically emit a Clang-compatible compile_commands.json on every
  mcpp build. The JSON includes all compile units with exact compiler
  commands, matching the flags used by the ninja backend.

- 5c1da99 feat: add .clang-format from mcpp-community/mcpp-style-ref

- 9675ea9 refactor(build): extract flags + compile_commands into dedicated modules
  Architecture redesign: flags.cppm (shared flag computation),
  compile_commands.cppm (JSON generation via nlohmann/json),
  json.cppm + json.hpp (nlohmann/json single-header as C++23 module).
  ninja_backend.cppm slimmed by ~200 lines.

- 0134a8b feat: namespace-aware read_xpkg_lua + bump to 0.0.8
  read_xpkg_lua now tries multiple filename patterns for package .lua
  descriptors in index repos.

- 2f482b7 ci: bump xlings 0.4.25 → 0.4.30 (sysroot fix for gcc 16.1.0)

- eba834d ci: always install pinned xlings (cache may hold stale version)

Co-authored-by: yizhinailong <liufeng.code@outlook.com>
Co-authored-by: sunrisepeak <speakshen@163.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants